package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.entity.UserToken;
import com.qfedu.openplateform.manager.service.TokenService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @RequestMapping("/sys/token/table")
    @ResponseBody
    public TableData getTokenByPage(Integer page, Integer limit, String accessToken) {
        PageHelper.startPage(page, limit);
        PageInfo<UserToken> userTokenPageInfo = null;
        if (accessToken == null || "" == accessToken) {
            userTokenPageInfo = new PageInfo<>(tokenService.getTokenAll());
        }else {
            userTokenPageInfo = new PageInfo<>(tokenService.getTokenByAccessToken(accessToken));
        }

        return new TableData(userTokenPageInfo.getTotal(), userTokenPageInfo.getList());
    }

    @RequestMapping("/sys/token/update")
    @ResponseBody
    public AjaxMessage updateToken(UserToken userToken) {

        try {
            if (tokenService.updateToken(userToken) != 0) {
                return new AjaxMessage(true);
            }
            return new AjaxMessage(false, "删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(false, "删除失败");
        }
    }




}
